package Client.View;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Client.Model.GameModel;

public class GamePlayView extends JPanel implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 942079744477606334L;

	GameModel model;
	int width;
	int height;
	
	JPanel top, center;
	JLabel meLbl, enemyLbl, gameStatus;
	
	
	public GamePlayView(GameModel model, int width, int height) {
		
		this.model = model;
		this.height = height;
		this.width = width;
		
		setSize(new Dimension(width, height));
		
		model.addObserver(this);
		
		setVisible(false);
		
		setView();
		
	}

	private void setView() {
	
		this.setPreferredSize(new Dimension(width, height));
		this.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		this.setLayout(null);
		
		top = new JPanel();
		center = new JPanel();
		
		meLbl = new JLabel();
		enemyLbl = new JLabel();
		gameStatus = new JLabel();

		top.add(gameStatus);
		top.add(meLbl);
		top.add(enemyLbl);
		
		top.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
		top.setBounds(0, 0, width, 90);
		
		center.setBackground(LoginFrame.DEFAULT_BG_COLOR);
		center.setBounds(0, 90, width, 380);
		
		center.add(new GamePlayCanvas(model, 350, 350, true));
		center.add(new GamePlayCanvas(model, 350, 350, false));
		
		this.add(top);
		this.add(center);
		
	}

	@Override
	public void update(Observable o, Object arg) {

		if (model.getGamestate()==GameModel.STATE_PLAYING || model.getGamestate()==GameModel.STATE_FINISHED){
			this.setVisible(true);
			
			String str = "";
			
			if (model.getGamestate()==GameModel.STATE_PLAYING)
				str = "Game Running...";
			else{
				if (model.getGamestate()==GameModel.STATE_FINISHED)
					str = "Game Finished!";
			}
				
			gameStatus.setText(str); 
			
			
			str = "Me => " + model.getUsername();
			if (model.isMyTurn())
				str += " (turn)";
			
			meLbl.setText(str);
			
			
			str = "Enemy => " + model.getEnemy();
			if (!model.isMyTurn())
				str += " (turn)";
			
			enemyLbl.setText(str);
			
			
		}
		else
			this.setVisible(false);
		
		revalidate();
		repaint();
	}

}
